package com.shanghaionstar.user.presenter;

import android.os.Bundle;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.PrepaidCardResponse;
import com.onstar.cn.vehicle.entity.ErrorEnum;
import com.onstar.cn.vehicle.entity.ErrorInfo;
import com.shanghaionstar.user.iview.IPayCardDetailView;
import com.shanghaionstar.user.service.PayCardDetailService;

import rx.functions.Action1;

/**
 * Created by Mengzhou Gu on 2016/8/2.
 */
public class PayCardDetailPresenter extends BasePresenter<PrepaidCardResponse> {
    private IPayCardDetailView   payCardDetailView;
    private PayCardDetailService service;
    private Bundle               bundle;

    public PayCardDetailPresenter(IMVPScheduler mvpScheduler, Bundle bundle) {
        super(mvpScheduler);
        this.bundle = bundle;
    }

    @Override
    public void setView(IMVPView<PrepaidCardResponse> view) {
        super.setView(view);
        service = new PayCardDetailService(view.onActivity(), bundle);
        payCardDetailView = (IPayCardDetailView) view;
        setServiceProvider(service);
    }

    public void getData() {
        payCardDetailView.setLoading(true);
        service.getObserval().subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<PrepaidCardResponse>() {
                    @Override
                    public void call(PrepaidCardResponse prepaidCardResponse) {
                        if (!CommonUtil.isNull(prepaidCardResponse)) {
                            payCardDetailView.setLoading(false);
                            payCardDetailView.showSuccess(prepaidCardResponse);
                        } else {
                            payCardDetailView.setLoading(false);
                            payCardDetailView.showFailure();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        payCardDetailView.setLoading(false);
                        Bundle bundle=new Bundle();
                        ErrorInfo errorInfo=new ErrorInfo();
                        errorInfo.setCode(ErrorEnum.SB027_MSG007.getStringValue());
                        errorInfo.setMsg("预付卡激活失败");
                        bundle.putSerializable("errorInfo",errorInfo);
                        payCardDetailView.showError(bundle,throwable);
                    }
                });
    }
}

